package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Banner;
import com.baizhi.service.BannerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @RequestMapping("insert")
    @ResponseBody
    public Map insert(Banner b){
        Map map = new HashMap();
        try{
            bannerService.insert(b);
            map.put("flag",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("delete")
    @ResponseBody
    public Map delete(Integer id){
        System.err.println(id);
        Map map = new HashMap();
        try{
            bannerService.delete(id);
            map.put("flag",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("update")
    @ResponseBody
    public Map update(Banner b){
        System.err.println(b);
        Map map = new HashMap();
        try{
            bannerService.update(b);
            map.put("flag",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("selectOne")
    @ResponseBody
    public Banner selectOne(Integer id){
        return bannerService.selectOne(id);
    }
    @LogAnnotation(content = "轮播图所有数据",type = "查询")
    @RequestMapping("selectAll")
    @ResponseBody
    public Map update(Integer page,Integer limit){
        Map map = new HashMap();
        PageInfo<Banner> info = bannerService.selectAll(page, limit);
        List<Banner> list = info.getList();
        map.put("code",0);
        map.put("msg","");
        map.put("count",info.getTotal());
        map.put("data",list);
        return map;
    }
//    文件上传
    @RequestMapping("addImg")
    @ResponseBody
    public Map addImg(MultipartFile file){
        Map map = new HashMap();
        try {
            file.transferTo(new File("D:\\img",file.getOriginalFilename()));
            map.put("flag",file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }
}
